# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import time

from openerp.osv import osv, fields
from openerp.tools.translate import _
from openerp import api
from openerp import fields as fields2
from openerp.tools import float_round, float_is_zero, float_compare
import openerp.addons.decimal_precision as dp

class account_invoice(osv.osv):

    _inherit = "account.invoice"

    def _shipyard_get(self, cr, uid, ids, field_name, arg, context=None):
        res = {}
        for account in self.browse(cr, uid, ids, context=context):
            data_ship = False
            for lines in account.invoice_line:
                if lines.type == 'shipyard':
                    data_ship = True
            res[account.id] = data_ship
        return res
    
    def _mechanical_get(self, cr, uid, ids, field_name, arg, context=None):
        res = {}
        for account in self.browse(cr, uid, ids, context=context):
            data_mech = False
            for lines in account.invoice_line:
                if lines.type == 'mechanical':
                    data_mech = True
            res[account.id] = data_mech
        return res
    
    def _electrical_get(self, cr, uid, ids, field_name, arg, context=None):
        res = {}
        for account in self.browse(cr, uid, ids, context=context):
            data_elec = False
            for lines in account.invoice_line:
                if lines.type == 'electrical':
                    data_elec = True
            res[account.id] = data_elec
        return res
    
    def _welding_get(self, cr, uid, ids, field_name, arg, context=None):
        res = {}
        for account in self.browse(cr, uid, ids, context=context):
            data_weld = False
            for lines in account.invoice_line:
                if lines.type == 'welding_fabrication':
                    data_weld = True
            res[account.id] = data_weld
        return res
    
    def _interior_get(self, cr, uid, ids, field_name, arg, context=None):
        res = {}
        for account in self.browse(cr, uid, ids, context=context):
            data_int = False
            for lines in account.invoice_line:
                if lines.type == 'interior':
                    data_int = True
            res[account.id] = data_int
        return res
    
    def _design_get(self, cr, uid, ids, field_name, arg, context=None):
        res = {}
        for account in self.browse(cr, uid, ids, context=context):
            data_desg = False
            for lines in account.invoice_line:
                if lines.type == 'design':
                    data_desg = True
            res[account.id] = data_desg
        return res
    
    def _dept1_get(self, cr, uid, ids, field_name, arg, context=None):
        res = {}
        for account in self.browse(cr, uid, ids, context=context):
            data_dept1 = False
            for lines in account.invoice_line:
                if lines.type == 'other_dept1':
                    data_dept1 = True
            res[account.id] = data_dept1
        return res
    
    def _dept2_get(self, cr, uid, ids, field_name, arg, context=None):
        res = {}
        for account in self.browse(cr, uid, ids, context=context):
            data_dept2 = False
            for lines in account.invoice_line:
                if lines.type == 'other_dept2':
                    data_dept2 = True
            res[account.id] = data_dept2
        return res
    
    def _dept3_get(self, cr, uid, ids, field_name, arg, context=None):
        res = {}
        for account in self.browse(cr, uid, ids, context=context):
            data_dept3 = False
            for lines in account.invoice_line:
                if lines.type == 'other_dept3':
                    data_dept3 = True
            res[account.id] = data_dept3
        return res
    
    def _total_discount_calculate(self, cr, uid, ids, field_name, arg, context=None):
        res = {}
        for account in self.browse(cr, uid, ids, context=context):
            ttl_discount = 0
            for lines in account.invoice_line:
                ttl_discount += (lines.price_unit * (lines.discount or 0.0) / 100.0)
            res[account.id] = ttl_discount
        return res
    
    def get_parent_partner_id(self, partner_id):
        new_partner_id = False
        if partner_id.parent_id:
            new_partner_id = partner_id.parent_id
        if new_partner_id:
            new_partner_id = self.get_parent_partner_id(new_partner_id)
        else:
            new_partner_id = partner_id
        return new_partner_id

    def _get_real_partner(self, cr, uid, ids, field_name, arg, context=None):
        result = {}
        for rec in self.browse(cr, uid, ids, context=context):
            new_partner_id = self.get_parent_partner_id(rec.partner_id)
            result[rec.id] = new_partner_id.id
        return result
    
    def _get_dept1_alias(self, cr, uid, ids, field_name, arg, context=None):
        result = {}
        alias_dept1 = ''
        
        for rec in self.browse(cr, uid, ids, context=context):
            if rec.company_id.dept1_alias:
                alias_dept1 = rec.company_id.dept1_alias
            else:
                alias_dept1 = 'Department1'
            result[rec.id] = alias_dept1
        return result
    
    def _get_dept2_alias(self, cr, uid, ids, field_name, arg, context=None):
        result = {}
        alias_dept2 = ''
        
        for rec in self.browse(cr, uid, ids, context=context):
            if rec.company_id.dept2_alias:
                alias_dept2 = rec.company_id.dept2_alias
            else:
                alias_dept2 = 'Department2'
            result[rec.id] = alias_dept2
        return result
    
    def _get_dept3_alias(self, cr, uid, ids, field_name, arg, context=None):
        result = {}
        alias_dept3 = ''
        
        for rec in self.browse(cr, uid, ids, context=context):
            if rec.company_id.dept3_alias:
                alias_dept3 = rec.company_id.dept3_alias
            else:
                alias_dept3 = 'Department3'
            result[rec.id] = alias_dept3
        return result
    
    _columns = {
            'dept1_alias': fields.function(_get_dept1_alias, type='char', string='Dept1'),
            'dept2_alias': fields.function(_get_dept2_alias, type='char', string='Dept2'),
            'dept3_alias': fields.function(_get_dept3_alias, type='char', string='Dept3'),
            'real_partner_id': fields.function(_get_real_partner, type='many2one', relation='res.partner', string='Partner'),
            'analytic_contract_id': fields.many2one('account.analytic.account', 'Contract', domain=[('state','!=','cancelled')]),
            'shipyard_data': fields.function(_shipyard_get, string='Shipyard', type='boolean'),
            'mechanical_data': fields.function(_mechanical_get, string='Mechanical', type='boolean'),
            'electrical_data': fields.function(_electrical_get, string='Electrical', type='boolean'),
            'welding_data': fields.function(_welding_get, string='Welding & Fabrication', type='boolean'),
            'interior_data': fields.function(_interior_get, string='Interior', type='boolean'),
            'design_data': fields.function(_design_get, string='Design', type='boolean'),
            'other_dept1_data': fields.function(_dept1_get, string='Other Dept1', type='boolean'),
            'other_dept2_data': fields.function(_dept2_get, string='Other Dept2', type='boolean'),
            'other_dept3_data': fields.function(_dept3_get, string='Other Dept3', type='boolean'),
            'total_discount': fields.function(_total_discount_calculate, string='Discount', type='float'),
            }
    
    def cust_invoice_print(self, cr, uid, ids, context=None):
        if context is None:
           context = {}
        data = self.read(cr, uid, ids)[0]
        self_browse = self.browse(cr, uid, ids)
        
        datas = {
            'ids': [data.get('id')],
            'model': 'account.invoice',
            'form': data
            }
        return {
            'type': 'ir.actions.report.xml',
            'report_name': 'batam_fast_module.ami_cust_invoice',
            'datas': datas,
            'name': self_browse.internal_number
            }
        
class account_invoice_line(osv.osv):
 
    _inherit = "account.invoice.line"
 
    _columns = {
            'analytic_line_id': fields.many2one('analytic.quotation.line', 'Stock Quotation Line'),
            'analytic_service_line_id': fields.many2one('analytic.service.quotation.line', 'Service Quotation Line'),
            'type':fields.selection([('shipyard','Shipyard'),
                                 ('mechanical','Mechanical'),
                                 ('electrical','Electrical'),
                                 ('welding_fabrication','Welding & Fabrication'),
                                 ('interior','Interior'),
                                 ('design','Design'),
                                 ('other_dept1','Other Dept1'),
                                 ('other_dept2','Other Dept2'),
                                 ('other_dept3','Other Dept3')
                                 ], 'Type', readonly=True),
            'ot_flag': fields.boolean('OT'),
            'labour_flag': fields.boolean('Labour'),
            }

class account_analytic_account(osv.osv):

    _inherit = 'account.analytic.account'
    
    _columns = {
            'analytic_invoice_ids':fields.one2many('account.invoice','analytic_contract_id','Analytic Contract Invoice',
            domain=[('state','!=','cancelled')]),
            }
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
